<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Shop_model extends CI_Model {
	function __construct(){
        parent::__construct();
    }
    
    function test($username,$userid){
    
    	echo "99";exit;
    	//$_sql = "insert into rsh_user (user_id,type_id,username,password,phone,paypassword,phone_status)
		//	values ($userid,2,'{$username}','9db06bcff9248837f86d1a6bcf41c9e7',18820153957,'9db06bcff9248837f86d1a6bcf41c9e7','1');";
    	
    	$_sql = "insert into rsh_account (id,user_id,total,use_money)
    		values($username,$userid,1000000,1000000);";
    	
    	$query = $this->db->query($_sql);
    }
    
    
    function arrays($key = ''){
    	$data = array(
    		'types' => array(
    			'0' => '--',
    			'1' => '办公类',
    			'2' => '电子类',
    			'3' => '生活类',
    		),
    		
    		'pstatus' => array(//商品状态[0撤销1上架]
    			'0'=> '撤销',
    			'1'=> '上架',
    		),
    		
    		'ptui' => array(//是否推荐[0否1是]
    			'0'=> '否',
    			'1'=> '是',
    		),
    		'ptuan' => array(//是否参团[0否1是]
    			'0'=> '否',
    			'1'=> '是',
    		),
    		'phot' => array(//是否热门[0否1是]
    			'0'=> '否',
    			'1'=> '是',
    		),
    		'prob' => array(//是否抢购[0否1是]
    			'0'=> '否',
    			'1'=> '是',
    		),
    	);
    	if (empty($key)){
    		return $data;
    	}elseif ($key && isset($data[$key])){
    		return $data[$key];
    	}else{
    		return false;
    	}
    }
    
    function pageData($data = array()){
    	$page = isset($data['page']) ? $data['page'] : 0;
    	$page_size = isset($data['page_size']) ? $data['page_size'] : 10;
    	$limit = $page;
    	
    	$order_by = isset($data['order_by']) ? $data['order_by'] : 'id';
    	$order_type = isset($data['order_by']) ? $data['order_by'] : 'asc';
    	
    	$_select = "*";
    	$tmp = array();
    	if (isset($data['where']['pname']) && $data['where']['pname'] != ''){
    		$tmp[] = "pname like '%{$data['where']['pname']}%'";
    	}
    	if (isset($data['where']['ptype']) && $data['where']['ptype']>0){
    		$tmp[] = "ptype={$data['where']['ptype']}";
    	}
    	if (isset($data['where']['pstatus']) && $data['where']['pstatus']>0){
    		$tmp[] = "pstatus={$data['where']['pstatus']}";
    	}
    	if (isset($data['where']['ptui']) && $data['where']['ptui']>0){
    		$tmp[] = "ptui={$data['where']['ptui']}";
    	}
    	if (isset($data['where']['ptuan']) && $data['where']['ptuan']>0){
    		$tmp[] = "ptuan={$data['where']['ptuan']}";
    	}
    	if (isset($data['where']['phot']) && $data['where']['phot']>0){
    		$tmp[] = "phot={$data['where']['phot']}";
    	}
    	if (isset($data['where']['prob']) && $data['where']['prob']>0){
    		$tmp[] = "prob={$data['where']['prob']}";
    	}
    	
    	$_where = $tmp ? implode(" and ", $tmp) : "1=1";
    	
    	$sql = "select $_select from rsh_shop_product where $_where order by $order_by $order_type limit $limit,$page_size";
    	
    	$query = $this->db->query($sql);
		$data['list'] = $query->result_array();
		
		$sql = "select count(id) as count from rsh_shop_product where $_where";
		$query = $this->db->query($sql);
		$data['count'] = $query->row()->count;
		
    	return $data;
    }
    
    function getOne($id){
    	$key = 'rsh_shop_product_' . $id;
		$this->mycache->delete($key);//@todo
		$row = $this->mycache->get_array($key);
		if($row == false){
			$this->db->where('id',$id);
			$query = $this->db->get('rsh_shop_product');
			$row = $query->row_array();
			if($row){
				$this->mycache->set_array($key,$row);
			}
		}
		return $row;
    }
    
    function add($data = array()){
    	$this->db->insert('rsh_shop_product', $data);
		return $this->db->insert_id();
    }
    
    function update($data = array(), $where = array()){
    	$this->db->where($where);
		$this->db->update('rsh_shop_product', $data);
		$res = $this->db->affected_rows();
		if($res > 0 && isset($where['id'])){
			$key = 'rsh_shop_product_' . $id;
			$this->mycache->delete($key);
		}
		return $res;
    }
    
    function getLastOrder($user_id = 0){
    	$sql = "select * from rsh_shop_order where user_id={$user_id} order by id desc limit 1";
    	$query = $this->db->query($sql);
		$row = $query->row_array();
		return $row;
    }
    
    function addOrder($data = array()){
    	$this->db->insert('rsh_shop_order', $data);
		return $this->db->insert_id();
    }
    
    function orderPageList($data = array()){
    	$page = isset($data['page']) ? $data['page'] : 0;
    	$page_size = isset($data['page_size']) ? $data['page_size'] : 10;
    	$limit = $page;
    		
    	$_select = "p1.*,p2.pname,p3.username,p4.kefu_userid";
    	
    	if (isset($data['order']) && $data['order']=='add_time'){
    		$order_by = "p1.add_time desc";
    	}else{
    		$order_by = "p1.fstatus asc,p1.id asc";
    	}
    	$tmpWhere = array();
    	if (isset($data['where']['id']) && $data['where']['id']){
    		$tmpWhere[] = "p1.id={$data['where']['id']}";
    	}
    	if (isset($data['where']['user_id']) && $data['where']['user_id']){
    		$tmpWhere[] = "p1.user_id={$data['where']['user_id']}";
    	}
    	if (isset($data['where']['username']) && $data['where']['username']){
    		$tmpWhere[] = "p3.username='{$data['where']['username']}'";
    	}
    	if (isset($data['where']['pid']) && $data['where']['pid']){
    		$tmpWhere[] = "p1.pid={$data['where']['pid']}";
    	}
    	if (isset($data['where']['fstatus']) && $data['where']['fstatus']!=-1){
    		$tmpWhere[] = "p1.fstatus={$data['where']['fstatus']}";
    	}
    	if (isset($data['where']['strtime']) && $data['where']['strtime']!=''){
    		$tmptime = strtotime($data['where']['strtime']);
    		$tmpWhere[] = "p1.add_time>={$tmptime}";
    	}
    	if (isset($data['where']['endtime']) && $data['where']['endtime']!=''){
    		$tmptime = strtotime($data['where']['endtime'])+3600*24;
    		$tmpWhere[] = "p1.add_time<{$tmptime}";
    	}
    	$_where = implode(' and ', $tmpWhere);
    	$_where = $_where ? " where ".$_where : '';
    	$sql = "select $_select from rsh_shop_order as p1 left join rsh_shop_product as p2 on p1.pid=p2.id left join rsh_user as p3 on p1.user_id=p3.user_id left join rsh_user_vip as p4 on p1.user_id=p4.user_id  $_where order by $order_by limit $limit,$page_size";
    	
    	$query = $this->db->query($sql);
		$data['list'] = $query->result_array();
		
		$sql = "select count(*) as count from rsh_shop_order as p1 left join rsh_shop_product as p2 on p1.pid=p2.id left join rsh_user as p3 on p1.user_id=p3.user_id left join rsh_user_vip as p4 on p1.user_id=p4.user_id  $_where";
		$query = $this->db->query($sql);
		$data['count'] = $query->row()->count;
		
    	return $data;
    }
    
	function updateOrder($data = array(), $where = array()){
		$this->mycache->delete('get_shop_order_show_index');
    	$this->db->where($where);
		$this->db->update('rsh_shop_order', $data);
		$res = $this->db->affected_rows();
		return $res;
    }
    
    function getMonthCost($user_id=0){
    	$time = time()-3600*24*30;
    	$sql = "select sum(total_price) as total_price from rsh_shop_order where user_id={$user_id} and add_time>{$time}";
    	$query = $this->db->query($sql);
		$row = $query->row_array();
		return $row['total_price'];
    }
    
    /**
     * 用户当天开盒子次数
     */
    function getTodayBoxNum($user_id=0){
    	$today = strtotime(date('Y-m-d'));
    	$sql = "select count(id) as num from rsh_shop_box_award where user_id={$user_id} and add_time>={$today}";
    	$query = $this->db->query($sql);
		$row = $query->row_array();
		return $row['num'];
    }
    
    /**
     * 初始化盒子
     */
    function initBox($data = array()){
    	$this->db->insert('rsh_shop_box_award', $data);
		return $this->db->insert_id();
    }
    
    /**
     * 获取盒子
     */
    function getBox($boxId = 0){
    	$this->db->where('id',$boxId);
		$query = $this->db->get('rsh_shop_box_award');
		$row = $query->row_array();
		return $row;
    }
    
    /**
     * 修改盒子
     */
	function updateBox($data = array(), $where = array()){
    	$this->db->where($where);
		$this->db->update('rsh_shop_box_award', $data);
		$res = $this->db->affected_rows();
		return $res;
    }
    
    function getBoxList($data = array()){
    	$page = isset($data['page']) ? $data['page'] : 0;
    	$page_size = isset($data['page_size']) ? $data['page_size'] : 10;
    	$limit = $page;
    		
    	$_select = "p1.*,p2.username,p3.pname";
    	
    	if (isset($data['order']) && $data['order']=='add_time'){
    		$order_by = "p1.add_time desc";
    	}else{
    		$order_by = "p1.fstatus asc,p1.id asc";
    	}
    	
    	$tmpWhere = array();
    	if (isset($data['where']['fstatus']) && $data['where']['fstatus']!=-1){
    		$tmpWhere[] = "p1.fstatus={$data['where']['fstatus']}";
    	}
    	
    	if (isset($data['where']['box_color']) && $data['where']['box_color']>0){
    		$tmpWhere[] = "p1.box_color={$data['where']['box_color']}";
    	}
    	
   		if (isset($data['where']['award_type']) && $data['where']['award_type']>0){
    		$tmpWhere[] = "p1.award_type={$data['where']['award_type']}";
    	}
    	
    	if (isset($data['where']['is_open']) && $data['where']['is_open']){
    		$tmpWhere[] = "p1.is_open={$data['where']['is_open']}";
    	}
    	if (isset($data['where']['username']) && $data['where']['username']){
    		$tmpWhere[] = "p2.username='{$data['where']['username']}'";
    	}
    	
    	if (isset($data['where']['strtime']) && $data['where']['strtime']!=''){
    		$tmptime = strtotime($data['where']['strtime']);
    		$tmpWhere[] = "p1.add_time>={$tmptime}";
    	}
    	if (isset($data['where']['endtime']) && $data['where']['endtime']!=''){
    		$tmptime = strtotime($data['where']['endtime'])+3600*24;
    		$tmpWhere[] = "p1.add_time<{$tmptime}";
    	}
    	if (isset($data['is_index']) && $data['is_index']==1){
    		$tmpWhere[] = "(p1.box_color<3 or p1.award_type=2)";
    	}
    	
    	$_where = implode(' and ', $tmpWhere);
    	$_where = $_where ? " where ".$_where : '';
    	
    	$sql = "select $_select from rsh_shop_box_award AS p1 left join rsh_user as p2 on p1.user_id=p2.user_id left join rsh_shop_product as p3 on p1.pid=p3.id  $_where order by $order_by";
    	
    	if(isset($data['limit']) && $data['limit']=='all'){
    		$query = $this->db->query($sql);
			$data['list'] = $query->result_array();
    		return $data;
    	}
    	
    	$sql.= " limit $limit,$page_size";
    	$query = $this->db->query($sql);
		$data['list'] = $query->result_array();
		
		$sql = "select count(*) as count from rsh_shop_box_award AS p1 left join rsh_user as p2 on p1.user_id=p2.user_id left join rsh_shop_product as p3 on p1.pid=p3.id  $_where";
		$query = $this->db->query($sql);
		$data['count'] = $query->row()->count;
		
    	return $data;
    	
    }
    
    function del(){
    	return false;
    }
    
    function count_shop_box_award($data){
    	
   		 if (isset($data['where']['box_color']) && $data['where']['box_color']>0){
    		$tmpWhere[] = "box_color={$data['where']['box_color']}";
    	}
   	
    	if (isset($data['where']['is_open']) && $data['where']['is_open']){
    		$tmpWhere[] = "is_open={$data['where']['is_open']}";
    	}
    	
    	if (isset($data['where']['is_specific']) && $data['where']['is_specific']){
    		$tmpWhere[] = "is_specific_num>0";
    	}
    	
    	if (isset($data['where']['strtime']) && $data['where']['strtime']!=''){
    		$tmptime = strtotime($data['where']['strtime']);
    		$tmpWhere[] = "add_time>={$tmptime}";
    	}
    	
    	
    	$_where = implode(' and ', $tmpWhere);
    	$_where = $_where ? " where ".$_where : '';
    	
    	$sql = "select * from rsh_shop_box_award $_where";
    	$query = $this->db->query($sql);
		$data['list'] = $query->result_array();
    	
    	$sql = "select count(*) as count from rsh_shop_box_award $_where";
		$query = $this->db->query($sql);
		$data['count'] = $query->row()->count;
    	return $data;
    }
    
    function get_shop_order_show_index(){
    	
    	$mkey = "get_shop_order_show_index";
		$ret = $this->mycache->get_array($mkey);
		if($ret != false){
			return $ret;
		}
		//封装查询内容
		$_select = "p1.*,p2.pname,p3.username,p4.kefu_userid";
		
		
		$_where = " where p1.fstatus = 1 and p1.is_show=1 ";
		
		
		$_order = " order by p1.id asc ";
		//封装sql
		$sql_result = "select $_select from rsh_shop_order as p1 left join rsh_shop_product as p2 on p1.pid=p2.id left join rsh_user as p3 on p1.user_id=p3.user_id left join rsh_user_vip as p4 on p1.user_id=p4.user_id  $_where $_order limit 10";
		
		$query = $this->db->query($sql_result);
		$shop_order_list = $query->result_array();
		$this->mycache->set_array($mkey,$shop_order_list);
		
		return $shop_order_list;
    }
    
	function get_shop_order_show_list($data=array()){
    	$page = isset($data['page_start']) ? $data['page_start'] : 0;
    	$page_size = isset($data['page_size']) ? $data['page_size'] : 10;
    	$limit = $page;
		
		//封装查询内容
		$_select = "p1.*,p2.pname,p3.username,p4.kefu_userid";
		
		
		$_where = " where p1.fstatus = 1 and p1.is_show=1 ";
		//封装条件
    	if (isset($data['where']['txt']) && $data['where']['txt']){
    		$_where .= " and (p2.pname like '%{$data['where']['txt']}%' or p3.username like '%{$data['where']['txt']}%')";
    	}
		
		$_order = " order by p1.id asc ";
		//封装sql
		$sql_result = "select $_select from rsh_shop_order as p1 left join rsh_shop_product as p2 on p1.pid=p2.id left join rsh_user as p3 on p1.user_id=p3.user_id left join rsh_user_vip as p4 on p1.user_id=p4.user_id  $_where $_order limit $limit,$page_size";
		
		
		$query = $this->db->query($sql_result);
		$data['shop_order_show_list'] = $query->result_array();
		
		$sql = "select count(*) as count from rsh_shop_order as p1 left join rsh_shop_product as p2 on p1.pid=p2.id left join rsh_user as p3 on p1.user_id=p3.user_id left join rsh_user_vip as p4 on p1.user_id=p4.user_id  $_where";
		$query = $this->db->query($sql);
		$data['article_count'] = $query->row()->count;
		
    	return $data;
    }
    
    
}